// 冒泡排序
// 时间复杂度: O(n^2)

Array.prototype.bubbleSort = function () {
  for (let i = 0; i < this.length - 1; i++) {
    for (let j = 0; j < this.length - i; j++) {
      // console.log(this[j], this[j + 1])
      if (this[j] > this[j + 1]) {
        const temp = this[j]
        this[j] = this[j + 1]
        this[j + 1] = temp
      }
    }
  }
}

const sortArr = (arr) => {
  let _arr = arr

  for (let i = 0; i < _arr.length - 1; i++) {
    for (let j = 0; j < _arr.length - i; j++) {
      if (_arr[j] > _arr[j + 1]) {
        const temp = _arr[j]
        arr[j] = arr[j + 1]
        arr[j + 1] = temp
      }
    }
  }

  return _arr
}

const arr = [5, 4, 3, 2, 1]
// arr.bubbleSort()
console.log(sortArr(arr))